/*
 * enc_sockmsg.h
 *
 *  Created on: 2018年11月6日
 *  Author: Degang.Wu (windragon@126.com)
 *  Desc:
 */

#include "enc_type.h"
#include "enc_shrm.h"

#ifndef ENC_SOCKMSG_H_
#define ENC_SOCKMSG_H_
typedef enum
{
	BWENC_MSG_INVALID	= 0,

	BWENC_MSG_REG2MST	=	1,
	BWENC_MSG_REPORT_STATUS,

	BWENC_MSG_QUERY_INFO,
	BWENC_MSG_UPDATE_CFG,			//更新一个通道配置
	BWENC_MSG_UPDATE_INITCFG,		//首次注册，下发所有通道配置
	BWENC_MSG_SYNC_SLV_TIME,		//同步校时

	BWENC_MSG_ACK,		// 正常ack
	BWENC_MSG_DENY,		// 拒绝（保留）

	BWENC_MSG_NB
}eMsgType;

typedef struct _msg_head
{
	U32		msg_type;
	U32		msg_len;	//buf len after this field
	U8		data[0];
}stMSG_HEAD, *pMSG_HEAD;

typedef struct _sync_time
{
	stMSG_HEAD	head;
	U32			sboard_idx;
	U32			timestamp;
}stSYNC_TIME, *pSYNC_TIME;

typedef struct _update_cfg
{
	U32				chl_idx;
	stENC_CHLSET	chl_set;
	U32				timestamp;
}stUPDATE_CFG, *pUPDATE_CFG;

typedef struct _init_cfg
{
	stMSG_HEAD		head;
	stENC_CHLSET	chlset[ENC_MAX_CHL];
	U32				timestamp;
}stINIT_CFG, *pINIT_CFG;

typedef struct _reg2mst
{
	stMSG_HEAD	head;
	U32			sboard_idx;
	U32			timestamp;
} stREG2MST, *pREG2MST;

typedef struct _CHL_INFO
{
	U32		errcode;
	U32		bitrate_kb;
}stCHL_INFO, *pCHL_INFO;

typedef struct _STS_INFO
{
	U32			sbidx;
	stCHL_INFO	chans[ENC_MAX_CHL];
}stSTS_INFO, *pSTS_INFO;

#define BW_NTOHS(val)	val = ntohs(val);
#define BW_HTONS(val) 	val = htons(val);
#define BW_NTOHL(val)	val = ntohl(val);
#define BW_HTONL(val)	val = htonl(val);

#define NTOH_MSGHEAD(p)		do {	\
	(p)->msg_type = ntohl((p)->msg_type);	\
	(p)->msg_len = ntohl((p)->msg_len);	\
} while(0);

#define HTON_MSGHEAD(p)		do {	\
	(p)->msg_type = htonl((p)->msg_type);	\
	(p)->msg_len = htonl((p)->msg_len);	\
} while(0);


#define NTOH_TM_SET(p) do {		\
	(p)->tm_start_sec = ntohl((p)->tm_start_sec);	\
	(p)->tm_end_sec = ntohl((p)->tm_end_sec);	\
}while(0);

#define NTOH_LOGO_SET(p) do {	\
	(p)->logo_width = ntohs((p)->logo_width);	\
	(p)->logo_height = ntohs((p)->logo_height);	\
	(p)->logo_x = ntohs((p)->logo_x);	\
	(p)->logo_y = ntohs((p)->logo_y);	\
}while(0);
#define NTOH_QR_SET(p) do {		\
	(p)->qr_opacity = ntohl((p)->qr_opacity);	\
	(p)->qr_width = ntohs((p)->qr_width);	\
	(p)->qr_height = ntohs((p)->qr_height);	\
	(p)->qr_x = ntohs((p)->qr_x);	\
	(p)->qr_y = ntohs((p)->qr_y);	\
}while(0);
#define NTOH_SUB_SET(p) 	do {	\
	(p)->sub_fontsize = ntohs((p)->sub_fontsize);	\
	(p)->sub_fontstyle = ntohs((p)->sub_fontstyle);	\
	(p)->sub_movdir = ntohs((p)->sub_movdir);	\
	(p)->sub_movspeed = ntohs((p)->sub_movspeed);	\
	(p)->sub_fontcolor = ntohl((p)->sub_fontcolor);	\
	(p)->sub_backcolor = ntohl((p)->sub_backcolor);	\
	(p)->sub_x = ntohs((p)->sub_x);	\
	(p)->sub_y = ntohs((p)->sub_y);	\
}while(0);
#define NTOH_OSD_SET(p) 	do {	\
	if ((p)->osd_type == BWENC_OSD_TYPE_TEXT)	\
	{	\
		NTOH_SUB_SET(&((p)->osd_cfg.osd_sub))	\
	}	\
	else if ((p)->osd_type == BWENC_OSD_TYPE_LOGO)	\
	{	\
		NTOH_LOGO_SET(&((p)->osd_cfg.osd_logo))	\
	} 	\
	else if ((p)->osd_type == BWENC_OSD_TYPE_LOGO)	\
	{	\
		NTOH_QR_SET(&((p)->osd_cfg.osd_qr))		\
	}	\
	for (int i_set = 0; i_set < ENC_WEEK_DAYS; i_set++)		\
	{	\
		for (int j_set = 0; j_set < ENC_OSD_MAX_TM_NUM; j_set++) \
		{ \
			NTOH_TM_SET(&((p)->osd_effect_tm[i_set][j_set]))	\
		} \
	}	\
}while(0);

#define NTOH_VID_SET(p) do {	\
	(p)->vid_width = ntohs((p)->vid_width);		\
	(p)->vid_height = ntohs((p)->vid_height);		\
	(p)->vid_bitrate_kb = ntohl((p)->vid_bitrate_kb);		\
	(p)->vid_bitrate_min = ntohl((p)->vid_bitrate_min);		\
	(p)->vid_bitrate_max = ntohl((p)->vid_bitrate_max);		\
	(p)->vid_gopsize = ntohs((p)->vid_gopsize);		\
	(p)->vid_fpsnum = ntohs((p)->vid_fpsnum);		\
	(p)->vid_fpsden = ntohs((p)->vid_fpsden);		\
}while(0);

#define NTOH_AUD_SET(p) do {	\
	(p)->aud_bitrate_kb = ntohs((p)->aud_bitrate_kb);	\
	(p)->aud_smp_rate	= ntohs((p)->aud_smp_rate);		\
}while(0);

#define NTOH_TS_SET(p)	do {	\
	(p)->ts_pmt_pid = ntohs((p)->ts_pmt_pid);	\
	(p)->ts_service_id = ntohl((p)->ts_service_id);	\
	(p)->ts_pcr_pid = ntohs((p)->ts_pcr_pid);	\
	(p)->ts_vid_pid = ntohs((p)->ts_vid_pid);	\
	(p)->ts_aud_pid = ntohs((p)->ts_aud_pid);	\
	(p)->ts_pcr_interval_ms = ntohs((p)->ts_pcr_interval_ms);	\
	(p)->ts_muxrate_kb = ntohl((p)->ts_muxrate_kb);	\
}while(0);

#define NTOH_ETHIO_SET(p) do { \
	(p)->ethio_sipport = ntohs((p)->ethio_sipport); \
	(p)->ethio_dipport = ntohs((p)->ethio_dipport); \
	(p)->ethio_sipaddr = ntohl((p)->ethio_sipaddr); \
	(p)->ethio_dipaddr = ntohl((p)->ethio_dipaddr); \
	(p)->ethio_bitrates = ntohl((p)->ethio_bitrates); \
}while(0);

#define NTOH_ENC_CHLSET(p)	do {	\
	NTOH_VID_SET(&((p)->encchl_vid));		\
	NTOH_AUD_SET(&((p)->encchl_aud));		\
	for (int k = 0; k < ENC_OSD_MAX_OVERLAY; k++)	\
	{ \
		NTOH_OSD_SET(&((p)->encchl_osd[k])) \
	} \
	NTOH_TS_SET(&((p)->encchl_ts));		\
	NTOH_ETHIO_SET(&((p)->encchl_output));	\
	(p)->encchl_errcode = ntohl((p)->encchl_errcode);	\
} while(0);


#define NTOH_UPDATE_CFG(p)	do {	\
	(p)->chl_idx = ntohl((p)->chl_idx);		\
	NTOH_ENC_CHLSET(&((p)->chl_set));	\
	NTOH_ENC_CHLSET(&((p)->chl_set))	\
	BW_NTOHL(p->timestamp)	\
}while(0);


#define HTON_TM_SET(p) do {		\
	(p)->tm_start_sec = htonl((p)->tm_start_sec);	\
	(p)->tm_end_sec = htonl((p)->tm_end_sec);	\
}while(0);

#define HTON_LOGO_SET(p) do {	\
	(p)->logo_width = htons((p)->logo_width);	\
	(p)->logo_height = htons((p)->logo_height);	\
	(p)->logo_x = htons((p)->logo_x);	\
	(p)->logo_y = htons((p)->logo_y);	\
}while(0);
#define HTON_QR_SET(p) do {		\
	(p)->qr_opacity = htonl((p)->qr_opacity);	\
	(p)->qr_width = htons((p)->qr_width);	\
	(p)->qr_height = htons((p)->qr_height);	\
	(p)->qr_x = htons((p)->qr_x);	\
	(p)->qr_y = htons((p)->qr_y);	\
}while(0);
#define HTON_SUB_SET(p) 	do {	\
	(p)->sub_fontsize = htons((p)->sub_fontsize);	\
	(p)->sub_fontstyle = htons((p)->sub_fontstyle);	\
	(p)->sub_movdir = htons((p)->sub_movdir);	\
	(p)->sub_movspeed = htons((p)->sub_movspeed);	\
	(p)->sub_fontcolor = htonl((p)->sub_fontcolor);	\
	(p)->sub_backcolor = htonl((p)->sub_backcolor);	\
	(p)->sub_x = htons((p)->sub_x);	\
	(p)->sub_y = htons((p)->sub_y);	\
}while(0);
#define HTON_OSD_SET(p) 	do {	\
	if ((p)->osd_type == BWENC_OSD_TYPE_TEXT)	\
	{	\
		HTON_SUB_SET(&((p)->osd_cfg.osd_sub))	\
	}	\
	else if ((p)->osd_type == BWENC_OSD_TYPE_LOGO)	\
	{	\
		HTON_LOGO_SET(&((p)->osd_cfg.osd_logo))	\
	} 	\
	else if ((p)->osd_type == BWENC_OSD_TYPE_LOGO)	\
	{	\
		HTON_QR_SET(&((p)->osd_cfg.osd_qr))		\
	}	\
	for (int i_tset = 0; i_tset < ENC_WEEK_DAYS; i_tset++)		\
	{	\
		for (int j_set = 0; j_set < ENC_OSD_MAX_TM_NUM; j_set++) \
		{ \
			HTON_TM_SET( &((p)->osd_effect_tm[i_tset][j_set]))	\
		} \
	}	\
}while(0);
/*
HTON_TM_SET(&((p)->osd_effect_tm[i][j]))	\
 */

#define HTON_VID_SET(p) do {	\
	(p)->vid_width = htons((p)->vid_width);		\
	(p)->vid_height = htons((p)->vid_height);		\
	(p)->vid_bitrate_kb = htonl((p)->vid_bitrate_kb);		\
	(p)->vid_bitrate_min = htonl((p)->vid_bitrate_min);		\
	(p)->vid_bitrate_max = htonl((p)->vid_bitrate_max);		\
	(p)->vid_gopsize = htons((p)->vid_gopsize);		\
	(p)->vid_fpsnum = htons((p)->vid_fpsnum);		\
	(p)->vid_fpsden = htons((p)->vid_fpsden);		\
}while(0);

#define HTON_AUD_SET(p) do {	\
	(p)->aud_bitrate_kb = htons((p)->aud_bitrate_kb);	\
	(p)->aud_smp_rate	= htons((p)->aud_smp_rate);		\
}while(0);

#define HTON_TS_SET(p)	do {	\
	(p)->ts_pmt_pid = htons((p)->ts_pmt_pid);	\
	(p)->ts_service_id = htonl((p)->ts_service_id);	\
	(p)->ts_pcr_pid = htons((p)->ts_pcr_pid);	\
	(p)->ts_vid_pid = htons((p)->ts_vid_pid);	\
	(p)->ts_aud_pid = htons((p)->ts_aud_pid);	\
	(p)->ts_pcr_interval_ms = htons((p)->ts_pcr_interval_ms);	\
	(p)->ts_muxrate_kb = htonl((p)->ts_muxrate_kb);	\
}while(0);

#define HTON_ETHIO_SET(p) do { \
	(p)->ethio_sipport = htons((p)->ethio_sipport); \
	(p)->ethio_dipport = htons((p)->ethio_dipport); \
	(p)->ethio_sipaddr = htonl((p)->ethio_sipaddr); \
	(p)->ethio_dipaddr = htonl((p)->ethio_dipaddr); \
	(p)->ethio_bitrates = htonl((p)->ethio_bitrates); \
}while(0);

#define HTON_ENC_CHLSET(p)	do {	\
	HTON_VID_SET(&((p)->encchl_vid));		\
	HTON_AUD_SET(&((p)->encchl_aud));		\
	for (int k = 0; k < ENC_OSD_MAX_OVERLAY; k++)	\
	{ \
		HTON_OSD_SET(&((p)->encchl_osd[k])) \
	} \
	HTON_TS_SET(&((p)->encchl_ts));		\
	HTON_ETHIO_SET(&((p)->encchl_output));	\
	(p)->encchl_errcode = htonl((p)->encchl_errcode);	\
} while(0);

#define HTON_UPDATE_CFG(p)	do {	\
	(p)->chl_idx = htonl((p)->chl_idx);		\
	HTON_ENC_CHLSET(&((p)->chl_set));	\
	HTON_ENC_CHLSET(&((p)->chl_set))	\
	BW_HTONL(p->timestamp)	\
}while(0);

#endif /* ENC_SOCKMSG_H_ */
